package com.example.rental_system.controller;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.rental_system.pojo.Admin;
import com.example.rental_system.pojo.House;
import com.example.rental_system.pojo.User;
import com.example.rental_system.service.AdminService;
import com.example.rental_system.service.HouseService;
import com.example.rental_system.service.UserService;
import com.example.rental_system.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;

    //管理员登陆
    @RequestMapping("/adminLogin")
    public String adminLogin(Admin admin, HttpSession session, Model model){
        //判断用户名密码是否正确
        QueryWrapper<Admin> qw=new QueryWrapper<>();
        qw.eq("admin_name",admin.getAdminName());
        Admin one = adminService.getOne(qw);
        if (one!=null && one.getPassword().equals(admin.getPassword())){
            session.setAttribute("admin",admin);
            session.setAttribute("adminName",admin.getAdminName());
            IPage<House> houseIPage=houseService.queryByUserId(0,1,3);
            int[] rainbow = PageUtil.rainbow(1, (int)houseIPage.getPages(), 3);
            model.addAttribute("pageInfo",houseIPage.getRecords());//页面信息
            model.addAttribute("pageNo",houseIPage.getCurrent());//当前页
            model.addAttribute("totalPages",houseIPage.getPages());//总页数
            model.addAttribute("rainbow",rainbow);
            return "main";
        }else {
            model.addAttribute("msg", ResultUtils.FAILURE);
            return "index";
        }

    }

    //进入后台管理系统
    @RequestMapping("/adminFragment")
    public String adminCenter(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        System.out.println(admin);
        return "adminFragment";
    }

    //前往修改密码
    @RequestMapping("/toChangePassword")
    public String toChangePassword(){
        return "changeAdminPassword";
    }
    //修改密码
    @RequestMapping("/changePassword")
    public String changePassword(String oldPassword,String newPassword,Model model, HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        //如果旧密码同session密码相同，执行修改
        if (oldPassword.equals(admin.getPassword())){
            admin.setPassword(newPassword);
            QueryWrapper qw=new QueryWrapper();
            qw.eq("admin_name",admin.getAdminName());
            adminService.update(admin,qw);
            return "redirect:/";
        }else {
            model.addAttribute("msg","原密码输入错误");
        }
        return "changeAdminPassword";
    }

    //前往用户信息展示
    @RequestMapping("/toShowAllUser")
    public String toUserShow(Model model){
        IPage<User> userIPage=userService.queryAllPage(1,5);
        int[] rainbow =PageUtil.rainbow(1,(int)userIPage.getTotal(),3);
        model.addAttribute("pageInfo",userIPage.getRecords());
        model.addAttribute("rainbow",rainbow);
        model.addAttribute("pageNo",userIPage.getCurrent());
        model.addAttribute("totalPages",userIPage.getPages());
        return "manageUsers";
    }


    //根据用户名删除用户
    @RequestMapping("/del/{userName}")
    public String delUserByUserName(@PathVariable String userName, Model model){
        //删除用户
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        userService.remove(queryWrapper);

        //上传数据
        IPage<User> userIPage=userService.queryAllPage(1,5);
        int[] rainbow =PageUtil.rainbow(1,(int)userIPage.getTotal(),3);
        model.addAttribute("pageInfo",userIPage.getRecords());
        model.addAttribute("rainbow",rainbow);
        model.addAttribute("pageNo",userIPage.getCurrent());
        model.addAttribute("totalPages",userIPage.getPages());
        return "manageUsers";
    }
    //用户分页
    @RequestMapping("/userPage/{page}")
    public String userPage(@PathVariable String page,Model model){
        IPage<User> userIPage=userService.queryAllPage(new Integer(page),5);
        int[] rainbow =PageUtil.rainbow(new Integer(page),(int)userIPage.getTotal(),3);
        model.addAttribute("pageInfo",userIPage.getRecords());
        model.addAttribute("rainbow",rainbow);
        model.addAttribute("pageNo",userIPage.getCurrent());
        model.addAttribute("totalPages",userIPage.getPages());

        return "manageUsers";
    }


    //前往发布房源
    @RequestMapping("/toSendHouse")
    public String toSendHouse(){
        return "adminSendHouse";
    }
    //发布房源
    @RequestMapping("/adminSendHouse")
    public String adminSendHouse(House house,MultipartFile file,HttpSession session){
        String url=houseService.upload(file);
        house.setUserId("0");
        house.setImg(url);
        house.setPublisher(session.getAttribute("adminName")+"-管理员");
        System.out.println(house);
        houseService.save(house);
        return "adminSendHouse";
    }

    //前往查看房源
    @RequestMapping("/toAdminHouse")
    public String toAdminHouse(Model model){
        IPage<House> houseIPage=houseService.queryAll(1,5);

        int[] rainbow =PageUtil.rainbow(1,(int)houseIPage.getTotal(),3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "adminHouse";
    }
    @RequestMapping("/housePage/{pageNo}")
    public String housePage(@PathVariable Integer pageNo,Model model){

        IPage<House> houseIPage=houseService.queryAll(pageNo,5);

        int[] rainbow =PageUtil.rainbow(pageNo,(int)houseIPage.getTotal(),3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "adminHouse";
    }
    @RequestMapping("/details/{id}")
    public String details(Model model,@PathVariable Integer id){
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        House one = houseService.getOne(queryWrapper);
        model.addAttribute("house",one);
        return "adminDetails";
    }
    @RequestMapping("/adminCsHouse")
    public String adminCsHouse(House house,MultipartFile file){
        System.out.println(house);
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",house.getId());
        houseService.update(house,queryWrapper);
        String url=houseService.updateHouse(house.getImg(),file);
        return "adminFragment";
    }
}
